package com.hrma.services.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hrma.dao.PostMapper;
import com.hrma.entity.Post;
import com.hrma.entity.PostDTO;
import com.hrma.entity.SelectResult;
import com.hrma.entity.exception.ContentExistException;
import com.hrma.entity.exception.MustHaveException;
import com.hrma.services.PostService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PostImpl implements PostService {
    private final PostMapper postMapper;

    public PostImpl(PostMapper postMapper) {
        this.postMapper = postMapper;
    }

    @Override
    public Post addPost(Post data) {
        if (postMapper.exist(null, data.getPostNum(), data.getPostName())) {
            throw new ContentExistException("该职务名称或编号已存在！");
        }
        postMapper.addPost(data);
        return data;
    }

    @Override
    public void deletePost(int id) {
        if (!postMapper.exist(id, null, null)) {
            throw new RuntimeException("id字段不存在！");
        }
        postMapper.deletePost(id);
    }

    @Override
    public void updatePost(Post data) {
        if (!postMapper.exist(data.getId(), null, null)) {
            throw new RuntimeException("id字段不存在！");
        }
        data.setPostName(data.getPostName().trim());
        postMapper.updatePost(data);
    }

    @Override
    public List<SelectResult> select() {
        return postMapper.select();
    }

    @Override
    public Page<Post> getList(PostDTO data) {
        PageHelper.startPage(data.getPageNum(),data.getPageSize());
        return postMapper.getList(data);

    }
}
